package com.work.yshop.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.work.yshop.R;
import com.work.yshop.adapter.FavoriteAdapter;
import com.work.yshop.model.CartItem;
import com.work.yshop.model.Favorite;
import com.work.yshop.model.Order;
import com.work.yshop.model.ProductHistory;
import com.work.yshop.mvp.contract.UserDataContract;
import com.work.yshop.mvp.presenter.UserDataPresenter;

import java.util.ArrayList;
import java.util.List;

/**
 * 收藏页面
 */
public class FavoriteActivity extends AppCompatActivity implements UserDataContract.View, FavoriteAdapter.OnItemClickListener {

    private Toolbar toolbar;
    private RecyclerView recyclerView;
    private View emptyView;
    private UserDataPresenter presenter;
    private FavoriteAdapter adapter;
    private List<Favorite> favoriteList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favorite);
        
        // 初始化Presenter
        presenter = new UserDataPresenter(this);
        presenter.attachView(this);
        
        // 初始化视图
        initViews();
        setupToolbar();
        setupRecyclerView();
        
        // 加载收藏数据
        presenter.loadFavoriteList();
    }
    
    private void initViews() {
        toolbar = findViewById(R.id.toolbar);
        recyclerView = findViewById(R.id.recycler_view);
        emptyView = findViewById(R.id.empty_view);
    }
    
    private void setupToolbar() {
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle("我的收藏");
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }
    
    private void setupRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new FavoriteAdapter();
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detachView();
        }
    }
    
    // FavoriteAdapter.OnItemClickListener 接口实现
    @Override
    public void onItemClick(int position) {
        Favorite favorite = favoriteList.get(position);
        if (favorite.getProduct() != null) {
            // 跳转到商品详情页
            Intent intent = new Intent(this, ProductDetailActivity.class);
            intent.putExtra("product_id", favorite.getProductId());
            startActivity(intent);
        }
    }
    
    @Override
    public void onRemoveClick(int position) {
        Favorite favorite = favoriteList.get(position);
        presenter.removeFavorite(favorite.getProductId());
    }
    
    @Override
    public void onAddToCartClick(int position) {
        Favorite favorite = favoriteList.get(position);
        if (favorite.getProduct() != null) {
            presenter.addToCart(favorite.getProduct(), 1);
        }
    }

    // UserDataContract.View接口实现
    @Override
    public void showLoading() {
        emptyView.setVisibility(View.VISIBLE);
        recyclerView.setVisibility(View.GONE);
    }

    @Override
    public void hideLoading() {
        emptyView.setVisibility(View.GONE);
    }

    @Override
    public void showError(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showHistoryList(List<ProductHistory> historyList) {
        // 不需要实现
    }

    @Override
    public void showFavoriteList(List<Favorite> favoriteList) {
        this.favoriteList = favoriteList;
        
        if (favoriteList.isEmpty()) {
            showEmptyView(1);
        } else {
            recyclerView.setVisibility(View.VISIBLE);
            emptyView.setVisibility(View.GONE);
            adapter.setData(favoriteList);
        }
    }

    @Override
    public void showCartItems(List<CartItem> cartItems) {
        // 不需要实现
    }

    @Override
    public void showOrderList(List<Order> orderList) {
        // 不需要实现
    }

    @Override
    public void showEmptyView(int type) {
        recyclerView.setVisibility(View.GONE);
        emptyView.setVisibility(View.VISIBLE);
    }

    @Override
    public void showResult(boolean success, String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        
        if (success) {
            // 如果操作成功（如取消收藏），刷新收藏列表
            presenter.loadFavoriteList();
        }
    }
} 